package com.panopset.flywheel.gui;

import static com.panopset.Util.x;

import java.awt.Color;
import java.awt.Graphics2D;

import com.panopset.Alert;
import com.panopset.centralen.util.MathUtil;

/**
 * Flywheel template API for graphics operations.
 *
 * @author Karl Dinwiddie
 *
 */
public final class Artist {

    /**
     * Graphics used to do the drawing.
     */
    private final Graphics2D g;

    /**
     * @param graphics
     *            Graphics used to do the drawing.
     */
    public Artist(final Graphics2D graphics) {
        g = graphics;
    }

    /**
     * Color format <b>rrggbb</b>.
     */
    private static final String COLOR_FORMAT = "rrggbb";

    /**
     * Color format map.
     */
    private static final int[][] CFM = {{0, 2 }, {2, 4 }, {4, 6 } };

    /**
     * Set color.
     *
     * @param colorRep
     *            Color representation in hex format <b>rrggbb</b>.
     */
    public void setColor(final String colorRep) {
        g.setColor(getColorFromRep(colorRep));
    }

    /**
     * Draw string.
     * @param s String to draw.
     * @param x X location.
     * @param y Y location.
     */
    public void drawString(final String s, final String x, final String y) {
        g.drawString(s, Integer.parseInt(x), Integer.parseInt(y));
    }

    /**
     * Fill rectangle.
     *
     * @param x
     *            X.
     * @param y
     *            Y.
     * @param w
     *            Width.
     * @param h
     *            Height.
     */
    public void drawRect(final String x, final String y, final String w,
            final String h) {
        g.drawRect(Integer.parseInt(x), Integer.parseInt(y), Integer
                .parseInt(w), Integer.parseInt(h));
    }

    /**
     * Fill rectangle.
     *
     * @param x
     *            X.
     * @param y
     *            Y.
     * @param w
     *            Width.
     * @param h
     *            Height.
     */
    public void fillRect(final String x, final String y, final String w,
            final String h) {
        g.fillRect(Integer.parseInt(x), Integer.parseInt(y), Integer
                .parseInt(w), Integer.parseInt(h));
    }

    /**
     * Fill rectangle at position 0, 0.
     *
     * @param w
     *            Width.
     * @param h
     *            Height.
     */
    public void fillRect(final String w, final String h) {
        g.fillRect(0, 0, Integer.parseInt(w), Integer.parseInt(h));
    }

    /**
     * Get color from hex representation. Red would be <b>ff0000</b>.
     *
     * @param colorRep
     *            Color representation in <b>rrggbb</b> format.
     * @return Color object that matches hex representation given.
     */
    public static Color getColorFromRep(final String colorRep) {
        if (colorRep == null || colorRep.length() != COLOR_FORMAT.length()) {
            Alert.red(x("Color representation not in hex") + " " + COLOR_FORMAT
                    + " " + x("format") + ": " + colorRep);
            return Color.RED;
        }
        final String rr = colorRep.substring(CFM[0][0], CFM[0][1]);
        final String gg = colorRep.substring(CFM[1][0], CFM[1][1]);
        final String bb = colorRep.substring(CFM[2][0], CFM[2][1]);
        final int r = Integer.parseInt(rr, MathUtil.HEX_RADIX);
        final int g = Integer.parseInt(gg, MathUtil.HEX_RADIX);
        final int b = Integer.parseInt(bb, MathUtil.HEX_RADIX);
        return new Color(r, g, b);
    }
}
